package com.bjsxt.service.impl;

import com.bjsxt.mapper.MenuMapper;
import com.bjsxt.pojo.Menu;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.TreeA;
import com.bjsxt.service.MenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<TreeA> selectMenuSice(Integer aid, Integer pid) {
        List<Menu> list = menuMapper.selectMenu(aid, pid);

        List<TreeA> list2= new ArrayList<>();

        for (Menu menu:list){
            TreeA t = new TreeA(menu.getMid(),menu.getMname(),menu.getStatus()==0?"closed":"open",menu.getUrl());
            list2.add(t);
        }
        return list2;
    }

    @Override
    public PageA<Menu> selectPagMenu(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);

        List<Menu> list = menuMapper.selectPagMenu();

        PageA<Menu> pgg=new PageA<>(list,pg.getTotal());

        return pgg;
    }

    @Override
    public List<TreeA> selectMenuSice2(Integer pid) {
        List<Menu> list = menuMapper.selectMenu2(pid);
        List<TreeA> list2= new ArrayList<>();
        for (Menu menu:list){
            TreeA t=new TreeA(menu.getMid(),menu.getMname(),menu.getStatus()==0?"closed":"open",menu.getUrl());
            list2.add(t);
        }
        return list2;
    }

    @Override
    public Integer insertMenuSice(Menu menu) {
        return menuMapper.insertMenu(menu);
    }

    @Override
    public Integer updateMenuSice(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public Integer deleteMenuSice(Integer mid) {
        List<Menu> list = menuMapper.selectMenu2(mid);
        if (list.size()>0){
            return -1;
        }

        return menuMapper.deleteMenu(mid);
    }

    @Override
    public List<TreeA> selectMenuSice3() {

        List<TreeA> list1 = menuMapper.selectMenu3(0);

        for (TreeA t:list1){
            Integer id = t.getId();

            List<TreeA> list2 = menuMapper.selectMenu3(id);

            t.setChildren(list2);
        }
        return list1;
    }
}
